package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.MaintenancePlans;
import com.ruoyi.system.domain.vo.MaintenancePlansVo;
import com.ruoyi.system.mapper.MaintenancePlansMapper;
import com.ruoyi.system.service.IMaintenancePlansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * 设备保养计划管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class MaintenancePlansServiceImpl implements IMaintenancePlansService {
    @Autowired
    private MaintenancePlansMapper maintenancePlansMapper;

    /**
     * 查询设备保养计划管理
     *
     * @param planId 设备保养计划管理主键
     * @return 设备保养计划管理
     */
    @Override
    public MaintenancePlans selectMaintenancePlansByPlanId(Long planId) {
        return maintenancePlansMapper.selectMaintenancePlansByPlanId(planId);
    }

    /**
     * 查询设备保养计划管理列表
     *
     * @param maintenancePlans 设备保养计划管理
     * @return 设备保养计划管理
     */
    @Override
    public List<MaintenancePlansVo> selectMaintenancePlansList(MaintenancePlans maintenancePlans) {
        List<MaintenancePlansVo> maintenancePlans1 = maintenancePlansMapper.selectMaintenancePlansList(maintenancePlans);
        for (MaintenancePlansVo plans : maintenancePlans1) {
            Date lastMaintenanceTime = plans.getLastMaintenanceTime();
            LocalDate localDate = lastMaintenanceTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            // 加上7天
            LocalDate newDate = localDate.plusDays(plans.getDaysUntilNext());
            // 如果需要转换回 Date 类型
            Date dateAfter7Days = Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            if (dateAfter7Days.compareTo(new Date()) > 0) {
                //查询还有剩几天进行保养

                // 获取今天的日期
                LocalDate today = LocalDate.now();

                // 将 dateAfter7Days 转换为 LocalDate
                LocalDate dateAfter7DaysLocal = dateAfter7Days.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

                // 计算两个日期之间的天数差异
                long daysDifference = ChronoUnit.DAYS.between(today, dateAfter7DaysLocal);

                plans.setDaysUntilNext1(daysDifference);


            } else {
                plans.setDaysUntilNext1(new Long(0));
            }


        }
        return maintenancePlans1;
    }

    /**
     * 新增设备保养计划管理
     *
     * @param maintenancePlans 设备保养计划管理
     * @return 结果
     */
    @Override
    public int insertMaintenancePlans(MaintenancePlans maintenancePlans) {
        maintenancePlans.setCreateTime(DateUtils.getNowDate());
        maintenancePlans.setLastMaintenanceTime(DateUtils.getNowDate());
        return maintenancePlansMapper.insertMaintenancePlans(maintenancePlans);
    }

    /**
     * 修改设备保养计划管理
     *
     * @param maintenancePlans 设备保养计划管理
     * @return 结果
     */
    @Override
    public int updateMaintenancePlans(MaintenancePlans maintenancePlans) {
        maintenancePlans.setUpdateTime(DateUtils.getNowDate());
        return maintenancePlansMapper.updateMaintenancePlans(maintenancePlans);
    }

    /**
     * 批量删除设备保养计划管理
     *
     * @param planIds 需要删除的设备保养计划管理主键
     * @return 结果
     */
    @Override
    public int deleteMaintenancePlansByPlanIds(Long[] planIds) {
        return maintenancePlansMapper.deleteMaintenancePlansByPlanIds(planIds);
    }

    /**
     * 删除设备保养计划管理信息
     *
     * @param planId 设备保养计划管理主键
     * @return 结果
     */
    @Override
    public int deleteMaintenancePlansByPlanId(Long planId) {
        return maintenancePlansMapper.deleteMaintenancePlansByPlanId(planId);
    }
}
